package org.jeecg.modules.mqtt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mqtt/demo")
public class MqttDemoController {

    @Autowired
    private MqttSubscriberService mqttSubscriberService;

    // 发送设备上传数据示例
    @GetMapping("/sendDeviceData")
    public String sendDeviceData() {
        String topicSuffix = "/device/upload";
        String message = "{\"deviceId\":\"DEV001\",\"temperature\":25.6,\"humidity\":60.2}";
        mqttSubscriberService.sendMessage(topicSuffix, message);
        return "Device data sent successfully";
    }

    // 发送传感器数据示例
    @GetMapping("/sendSensorData")
    public String sendSensorData() {
        String topicSuffix = "/sensor/temperature/data";
        String message = "26.3";
        // 使用指定QoS发送消息
        mqttSubscriberService.sendMessage(topicSuffix, message, 2);
        return "Sensor data sent successfully";
    }

    // 发送控制命令示例
    @GetMapping("/sendControlCommand")
    public String sendControlCommand() {
        String topicSuffix = "/control/device/switch";
        String message = "{\"deviceId\":\"DEV001\",\"command\":\"turnOn\"}";
        mqttSubscriberService.sendMessage(topicSuffix, message);
        return "Control command sent successfully";
    }
}
